package com.zhongwang.business.module.main.controller;

import com.zhongwang.business.base.BaseController;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.module.main.model.*;
import com.zhongwang.business.module.main.service.ProjectChangeQueryService;
import com.zhongwang.business.module.system.log.LogService;
import com.zhongwang.business.util.FileUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 项目变更查询
 * @author zcy
 * @date 20210409
 */
@Controller
public class ProjectChangeQueryController extends BaseController {

    @Autowired
    private ProjectChangeQueryService projectChangeQueryService;

    @Autowired
    private LogService logService;

    @GetMapping("/overview/projectChangeQuery")
    @RequiresPermissions("overview:projectChangeQuery:view")
    public String page(){
        return "main/proChgList/projectChangeList";
    }
    /**
     * 查询项目变更
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/projectScheduling/selectProjectChangeList")
    public JsonResult selectProjectChangeList(ProjectSchedulingModel model) {
        startPage();
        return jsonResult(projectChangeQueryService.selectProjectChangeList(model));
    }

    /**
     * 变更通知查看页面
     * @param proChangeId
     * @param model
     * @return
     */
    @GetMapping("/projectChange/chgInfo/viewInfo/{proChangeId}")
    public String viewInfoPage(@PathVariable("proChangeId") Integer proChangeId, Model model) {
        ProjectModel projectChgInfo = projectChangeQueryService.selectChangeInfoById(proChangeId);
        model.addAttribute("projectChgInfo", projectChgInfo);
        model.addAttribute("proChangeId", proChangeId);
        ProChange proChange = projectChangeQueryService.selectByPrimaryKey(proChangeId);
        if (proChange.getPreEnqId() != null){
            PreEnquiry preEnquiry = projectChangeQueryService.selectPreEnqById(proChange.getPreEnqId());
            model.addAttribute("preFiles", projectChangeQueryService.findChangeFileListByEnquiryId(preEnquiry.getId(), preEnquiry.getOrigId()));
        }
        return "main/proChgList/chgInfoPage";
    }

    /**
     * @methodName: downloadFile
     * @description: 变更查询下载
     */
    @GetMapping("proChangeQuery/downloadFile/{id}")
    @RequiresPermissions("overview:projectChangeQuery:download")
    public void downloadFile(@PathVariable("id") Integer id, HttpServletResponse httpServletResponse){
        VwPreEnquiryFile vwPreEnquiryFile = projectChangeQueryService.getVwPreEnquiryFileByPrimary(id);
        String filename = vwPreEnquiryFile.getUrl();
        filename = "e:\\sale\\upload\\"+filename.substring(7).replace("/","\\");
        FileUtils.downloadFile(filename, httpServletResponse);
        logService.saveLog(
                "项目变更查询-->变更通知单-->下载附件",
                "");
    }
}
